package com.ncloud.resman.mapper;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.resman.entity.DeviceServer;
import com.ncloud.resman.entity.ExportServer;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * Created by shiqiang on 2017/8/14.
 */
@Mapper
public interface DeviceServerMapper {
    @Insert("insert into res_device_server(device_id, purpose, manage_ip, ipmi_addr, sys_model, service_code, host_name," +
            "create_user,create_date,update_user,update_date)"
            + " values(#{deviceId},#{purpose},#{manageIp},#{ipmiAddr},#{sysModel},#{serviceCode},#{hostName}," +
            "#{createUser},#{createDate},#{updateUser},#{updateDate})")
    int insertDeviceServer(DeviceServer deviceServer);
    @Update("update res_device_server set purpose = #{purpose},manage_ip=#{manageIp},ipmi_addr =#{ipmiAddr},sys_model =#{sysModel}," +
            "service_code =#{serviceCode},host_name =#{hostName},update_user=#{updateUser},update_date=#{updateDate} where device_id =#{deviceId}")
    int updateDeviceServer(DeviceServer deviceServer);

    /*删除*/
    @Delete(value = "delete from res_device_server WHERE device_id = #{0}")
    public int deleteDeviceServer(Long deviceId);
    
    @Select("SELECT CONCAT(device_id,'') as device_id, purpose, CONCAT(manage_ip,'') as manage_ip, ipmi_addr, sys_model, service_code, host_name "
			+ "FROM res_device_server where device_id = #{deviceId}")
	@Results({
		@Result(column="device_id",property="deviceId"),@Result(column="purpose",property="purpose"),
		@Result(column="manage_ip",property="manageIp"),@Result(column="ipmi_addr",property="ipmiAddr"),
		@Result(column="sys_model",property="sysModel"),@Result(column="service_code",property="serviceCode"),
		@Result(column="host_name",property="hostName")})
	List<JSONObject> getDeviceServerById(Long deviceId);//根据设备id查询设备服务器信息
    @Select("SELECT * FROM res_device_server where device_id = #{deviceId}")
    @Results({
            @Result(column="device_id",property="deviceId"),@Result(column="purpose",property="purpose"),
            @Result(column="manage_ip",property="manageIp"),@Result(column="ipmi_addr",property="ipmiAddr"),
            @Result(column="sys_model",property="sysModel"),@Result(column="service_code",property="serviceCode"),
            @Result(column="host_name",property="hostName"),@Result(column="create_user",property="createUser"),
            @Result(column="create_date",property="createDate"),@Result(column="update_user",property="updateUser"),
            @Result(column="update_date",property="updateDate")})
    DeviceServer getDeviceServerByDeviceId(Long deviceId);
    
    //通过SNNUM查询服务器
    @Select("select a.device_id,a.model_name,a.sn_num,a.model_id,a.brand_id,a.brand_name,a.eqpt_spec,a.spec_name,"
    		+ "f_get_dict('eqpt_state',a.state) as state,b.room_id,b.room_name,b.cabinet_code,b.start_num,b.end_num,c.purpose,c.ipmi_addr,c.manage_ip"
    		+ ",c.sys_model,c.service_code,c.host_name from res_device_base a,res_device_location b,"
    		+ "res_device_server c where a.device_id=b.device_id and b.device_id=c.device_id and a.eqpt_type='1' and "
    		+ "if(#{0} is null,1=1,a.sn_num=#{0})")
    @Results({
		@Result(column="device_id",property="deviceId"),@Result(column="model_name",property="modelName"),
		@Result(column="sn_num",property="snNum"),@Result(column="model_id",property="modelId"),
		@Result(column="brand_id",property="brandId"),@Result(column="brand_name",property="brandName"),
		
		@Result(column="eqpt_spec",property="eqptSpec"),@Result(column="spec_name",property="specName"),
		@Result(column="state",property="state"),@Result(column="room_id",property="roomId"),
		@Result(column="room_name",property="roomName"),@Result(column="cabinet_code",property="cabinetCode"),
		
		@Result(column="start_num",property="startNum"),@Result(column="end_num",property="endNum"),
		@Result(column="purpose",property="purpose"),@Result(column="ipmi_addr",property="ipmiAddr"),
		@Result(column="manage_ip",property="manageIp"),@Result(column="sys_model",property="sysModel"),		
		@Result(column="service_code",property="serviceCode"),@Result(column="host_name",property="hostName")})
    List<ExportServer> getServerInfoBySnNum(String snNum);
    
}
